/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.unal.visor.view;

import co.unal.visor.model.*;
import co.unal.visor.util.*;

import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.ScrollPane;

import filter.Transformation2D;

/**
 * 
 * @author coco
 */
public class Lienzo extends Canvas implements MouseMotionListener {

	private static final long serialVersionUID = 1L;
	public Pixel[][] LPixelMatrix = null;
	public Image image = null;
	@SuppressWarnings("unused")
	private boolean repaint = true;
	Graphics g;
	private int width = 480;
	private int height = 640;
	Viewer visor;
	int grado_rot=0;

	public Lienzo() {
	}

	public Lienzo(final Viewer visor) {
		this.setBackground(Color.white);
		LPixelMatrix = new Pixel[height][width];
		this.setVisible(true);
		for (int i = 0; i < this.height; i++) {
			// LPixelMatrix[i] = new Pixel[width];
			// for(int j = 0; j < this.width; j++){}
		}

		this.addMouseListener(new MouseListener() {

			@Override
			public void mouseClicked(MouseEvent arg0) {

				setRotationWindow();
				setSize((int) Math.sqrt(Math.pow(image.getHeight(), 2)
						+ Math.pow(image.getWidth(), 2)) + 8,
						(int) Math.sqrt(Math.pow(image.getHeight(), 2)
								+ Math.pow(image.getWidth(), 2)) + 60);
				
			}

			@Override
			public void mouseEntered(MouseEvent arg0) {
				// TODO Auto-generated method stub
			}

			@Override
			public void mouseExited(MouseEvent arg0) {
				// TODO Auto-generated method stub
			}

			@Override
			public void mousePressed(MouseEvent arg0) {
				// TODO Auto-generated method stub
			}

			@Override
			public void mouseReleased(MouseEvent arg0) {
				// TODO Auto-generated method stub

			}

		});
	}

	@Override
	public void paint(Graphics g) {
		// this.setSize(this.width, this.height);
		paintImage(g);
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public Pixel[][] getLPixelMatrix() {
		return LPixelMatrix;
	}

	// copia la imagen desde el pixelMatrix a el LPixelMatrix de lienzo
	public boolean setPMatrixFromImage(Image image) {
		repaint = false;
		this.image = image;
		// redimensiona a un lienzo de tama�o igual a la diagonal de la imagen
		this.setWidth((int) Math.sqrt(Math.pow(image.getHeight(), 2)
				+ Math.pow(image.getWidth(), 2)) + 2);
		this.setHeight((int) Math.sqrt(Math.pow(image.getHeight(), 2)
				+ Math.pow(image.getWidth(), 2)) + 2);
		// inicializa LPixelMatrix
		LPixelMatrix = new Pixel[height][width];
		System.out.println("Image: alto " + image.getHeight() + " ancho "
				+ image.getWidth());

		Pixel background = new RGB();
		Pixel[][] pixelMatrixTemp = image.createPixelMatrix();

		for (int i = 0; i < height; i++) {
			for (int j = 0; j < width; j++) {
				if (image.getHeight() > i && image.getWidth() > j) {
					LPixelMatrix[i][j] = pixelMatrixTemp[i][j];
				} else {
					LPixelMatrix[i][j] = background;
				}
			}
		}
		return repaint = true;
	}

	public void paintImage(Graphics g2) {

		BufferedImage imagen = new BufferedImage(getWidth(), getHeight(),
				BufferedImage.TYPE_INT_RGB);
		Graphics g = imagen.getGraphics();

		this.setBackground(Color.white);
		this.setVisible(true);
		for (int i = 0; i < LPixelMatrix.length; i++) {
			for (int j = 0; j < LPixelMatrix[0].length; j++) {

				if (LPixelMatrix[i][j] == null) {
					System.out.println("valor de pixel nulo en " + i + " " + j);
					break;
				}

				g.setColor(new Color(this.LPixelMatrix[i][j].getRed(),
						this.LPixelMatrix[i][j].getGreen(),
						this.LPixelMatrix[i][j].getBlue()));

				g.drawLine(j, i, j, i);
			}
		}
		g2.drawImage(imagen, 0, 0, null);
	}

	@Override
	public void mouseDragged(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseMoved(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	public  void setRotationWindow() {
		
		grado_rot = grado_rot + 5;
		System.out.println("gradorotation=" + grado_rot);
		Transformation2D.rotateCenteredImage(image, grado_rot, this);
		//setPMatrixFromImage(image);
		updateLienzo();
		this.repaint();

	

	}
	
	public void updateLienzo() {
	
				 setSize((int) image.getWidth(), (int) image.getHeight() + 50);

			
		}

	


}
